// packageA/pages/operate/demonstration/demonstration.js
const App = getApp()
// let reg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1})|(19[0-9]{1}))+\d{8})$/;  //手机号正则表达式
let reg = /^1[0-9]{10}$/;
var reg_em = new RegExp("^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$"); //邮箱正则表达式
import moment from '../../../../utils/moment'
import Toast from '../../../../weapp/toast/toast';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // list: ['申请演示', '开户申请','申请结佣'],
    AllDepartment:[],
    AllDepartmentShow:false,
    AllDepartmentValue:'',
    // levelList:'',
    form:{
      name:'', 
      mobile:'',
      department_id:'',
      position:'',
      office_location:'',
      director:'',
      checkin_time:'',
    },
   
    fileList:[],
    nameR:'',
   
    show1:false,
    currentIndex: 0,
    api_root:'',
    
    currentDate: new Date().getTime(),
    minDate: new Date().getTime(),
    formatter(type, value) {
      if (type === 'year') {
        return `${value}年`;
      } 
      if (type === 'month') {
        return `${value}月`;
      }
      if (type === 'day') {
        return `${value}日`;
      }
      if (type === 'hour') {
        return `${value}时`;
      }
      if (type === 'minute') {
        return `${value}分`;
      }
      return value;
    },


  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.log(options)
    var api_root = wx.getStorageSync('api_root');
    this.setData({
      api_root:api_root
    })
    this.allDepartment();
  },

  //获取部门列表
  allDepartment(){
    let _this = this;
    App._post_form('AllDepartment/getList', {
      // action:'account_open_list'
    }, function (result) {
      // console.log(result);
      if (result.code === 0) {
        // _this.setData({
        //   AllDepartment:result.data
        // })
        let arr = result.data
        let newArr = []
        for (let index = 0; index < arr.length; index++) {
          let obj = {
            name:arr[index].name,
            id:arr[index].id
          }
          newArr.push(obj)
        }
        _this.setData({
          AllDepartment:newArr
        })
      }
    });
  },
  //部门选择展示
  AllDepartmentShow() {
    this.setData({
      AllDepartmentShow: true
    })
  },
  //部门选择关闭
  onCloseAllDepartment() {
    this.setData(
      { 
        show1:false,
        AllDepartmentShow:false
      }
      );
  },
  //部门点击选择
  onSelectAllDepartment(event) {
    // console.log(event.detail.id);
    this.setData({
      AllDepartmentValue:event.detail.name,
      // ...this.data.form,
      'form.department_id': event.detail.id,
    })
    // console.log(this.data.form);
  },

  //等级框与日期框的切换
  handleInput(e) {
    // let { type, input } = e.currentTarget.dataset
    // console.log(e)
    let time2 = new Date().getTime()
    this.setData({
      show: true,
      // showIndex: -1,
      // type: bol,
      // 'form.checkin_time':  moment(time2).format("YYYY-MM-DD"),
    })
  },

  onCancel() {
    this.setData({
      show: false
    })
  },

  onInput(event) {
    this.setData({
      currentDate: event.detail,
    });
    this.setData({
    //   time: moment(event.detail).format("YYYY-MM-DD hh:mm:ss"),
    //   ...this.data.form,
      // ...this.data.follow_form,
      // ...this.data.deal_form,
      // 'deal_form.order_date': this.data.time_assign_type == 'order_date' ? moment(event.detail).format("YYYY-MM-DD HH:mm:ss") : this.data.deal_form.order_date,
      // 'deal_form.down_date': this.data.time_assign_type == 'down_date' ? moment(event.detail).format("YYYY-MM-DD HH:mm:ss") : this.data.deal_form.down_date,
      // 'deal_form.loan_date': this.data.time_assign_type == 'loan_date' ? moment(event.detail).format("YYYY-MM-DD HH:mm:ss") : this.data.deal_form.loan_date,
      // 'remind_form.remind_time': this.data.time_assign_type == 'remind_time' ? moment(event.detail).format("YYYY-MM-DD HH:mm:ss") : this.data.remind_form.remind_time,
      'form.checkin_time': moment(event.detail).format("YYYY-MM-DD") 
    });
  },

  onConfirm(event) {
    console.log(event)
    // let type = event.currentTarget.dataset.type
    // let { value, index } = event.detail
    // console.log(111)
    this.setData({
      // [type]: value,
      show: false,
      'form.checkin_time': moment(event.detail).format("YYYY-MM-DD") 
      // follow_form: {
      //   ...this.data.follow_form,
      //   [type]: type == 'level' ? value.substr(0, 1) : value,
      // },
      // deal_form: {
      //   ...this.data.deal_form,
      //   [type]: value
      // },
    })
  },

  handleSelect() {
    this.setData({
      show: true
    })
  },
  // levelSelect() {
  //   this.setData({
  //     levelshow: true
  //   })
  // },
  onClose() {
    this.setData(
      { 
        show: false,
        show1:false,
        levelshow:false
      }
      );
  },

  onSelect(event) {
    if(this.data.currentIndex == 0){
      this.setData({
        ...this.data.follow_form,
        ...this.data.open_form,
        'follow_form.company_num': event.detail.name,
        // 'follow_form.level': event.detail.name,
        'open_form.company_num': '',
      })
    }else if(this.data.currentIndex == 1){
      console.log(event)
      this.setData({
        ...this.data.follow_form,
        ...this.data.open_form,
        'follow_form.company_num': '',
        'open_form.company_num': event.detail.name,
      })
    }
  },

  onSelect1(event) {
    this.setData({
      nameR:event.detail.name,
      ...this.data.open_form,
      'open_form.id': event.detail.id,
    })
    console.log(this.data.nameR);
  },
  // onSelectlevel(event) {
  //   console.log(event);
  //   this.setData({
  //     levelList:event.detail.name,
  //     ...this.data.follow_form,
  //     'follow_form.p_id': event.detail.id,
  //   })
  // },

  onChange(event) {
    console.log(event)
    this.setData({
      ...this.data.open_form,
      'open_form.business_type': event.detail,
      result: event.detail
    });
  },

  toggle(event) {
    const { index } = event.currentTarget.dataset;
    const checkbox = this.selectComponent(`.checkboxes-${index}`);
    checkbox.toggle();
  },

  handleSaveForm() {

    let _this = this
    let {name, mobile,department_id,position,office_location,director } = _this.data.form
    
    if (name == '') { Toast.fail('姓名不能为空');return false;}
    if (!reg.test(mobile)) { Toast.fail('手机号不符合要求'); return false;}
    if (department_id == '') { Toast.fail('请选择部门');return false;}
    if (position == '') { Toast.fail('职位不能为空');return false;}
    if (office_location == '') { Toast.fail('办公地点不能为空');return false;}
    if (director == '') { Toast.fail('直属主管不能为空');return false;}
    App._post_form('Induction/add', _this.data.form, function (result) {
      // console.log(result);
      // return false;
      if (result.code === 0) {
        Toast({
          type: 'success',
          message: '提交成功',
          onClose: () => {
            wx.reLaunch({
              url:'../../social/index/index'
            })
          },
        });
      } else {
        Toast.fail(result.msg);
      }
    });
    
  },
  inputOnChange(e) {
    let input = e.currentTarget.dataset.input
    let value = e.detail
    this.setData({
      form: {
        ...this.data.form,
        [input]: value
      },
    })
  },
  inputOnChange1(){
    this.setData({
      show1:true
    })
  },

//图片上传
afterRead(event) {
  let api_root = wx.getStorageSync('api_root')
    if (api_root) {
      App.api_root = api_root;
    }
  const { file } = event.detail;
  console.log(file)
  // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
  let _this = this
  let imgList = []
  wx.uploadFile({
    url: App.api_root+'Customer/imageUpload', // 仅为示例，非真实的接口地址
    filePath: file.url,
    name: 'proof',
    success(res) {
      console.log(res)
      // 上传完成需要更新 fileList

      const { fileList = [] } = _this.data;
      fileList.push({ ...file, url: JSON.parse(res.data).data.url });
      imgList = fileList.map(item => {

        return item.url
      })
      imgList = imgList.join(',')
      console.log(imgList)
      _this.setData({
        fileList,
        ..._this.data.form,
        'form.avatar': imgList
      });
    },
  });
},
   //删除
   deleteImg(e) {
    const index = e.detail.index
    let imgList = []
    this.data.fileList.splice(index, 1)
    imgList = this.data.fileList.map(item => {
      return item.url
    })
    imgList = imgList.join(',')
    console.log(imgList)
    this.setData({                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
      fileList: this.data.fileList,
      ...this.data.summary_form,
      'summary_form.proof': imgList
    })
    console.log(e, this.data.fileList)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})